/*
 * 广告位列表
 * @Author: ice
 * @Date: 2019-04-29 11:03:55
 * @Last Modified by: Wilbur
 * @Last Modified time: 2019-04-30 09:04:15
 */

<template>
    <div class="container activity-list-container">
        <div class="content-wrapper">
            <el-row
                class="search-wrapper"
                :gutter="20"
            >
                <el-form
                    label-position="right"
                    label-width="0"
                    ref="searchfrom"
                    size="small"
                >
                    <el-form-item>
                        <el-input
                            v-model.trim="filterData.search_content"
                            placeholder="请输入广告主名称"
                            type="text"
                            size="small"
                            style="width: 193px;"
                            @keyup.native.enter="getTableData(1)"
                        />
                    </el-form-item>
                    <el-button
                        size="small"
                        type="primary"
                        style="margin-left: 18px;"
                        @click="getTableData(1)"
                    >搜索</el-button>
                </el-form>
            </el-row>
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                >
                    <el-table-column
                        fixed="left"
                        header-align="left"
                        :width="50"
                        label="序号"
                    >
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="left"
                        prop="hire_info.company_name"
                        :min-width="150"
                        label="广告主名称"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        fixed="left"
                        prop="hire_info.phone"
                        :width="110"
                        label="联系方式"
                    />
                    <el-table-column
                        prop="banner"
                        :width="100"
                        label="广告位图片"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="fnShowPreview(scope.row.banner)"
                                :disabled="!scope.row.banner"
                            >查看</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="jump_title"
                        :min-width="100"
                        label="按钮名称"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        fixed="left"
                        prop="jump_url"
                        :width="200"
                        :formatter="emptyFormatter"
                        label="按钮链接"
                    >
                        <template slot-scope="scope">
                            <el-button
                                v-if="scope.row.jump_url"
                                type="text"
                                @click="fnOpen(scope.row.jump_url)"
                            >{{ scope.row.jump_url }}</el-button>
                            <span v-else>--</span>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="create_time"
                        :width="150"
                        label="创建时间"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.create_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="activity_title"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        label="赞助活动"
                        :min-width="150"
                    />

                    <el-table-column
                        prop="verify"
                        label="备注"
                        :min-width="110"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            <template v-if="scope.row.verify == 1">未审核</template>
                            <template v-if="scope.row.verify == 2">审核通过</template>
                            <template v-if="scope.row.verify == 3">不通过: {{ scope.row.verify_fail_reason }}</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="date"
                        label="操作"
                        fixed="right"
                        :width="110"
                    >
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    type="text"
                                    @click="fnVerifyPromot(scope.row.id, 2)"
                                    :disabled="scope.row.verify == 2"
                                >通过</el-button>
                                <el-button
                                    type="text"
                                    :disabled="scope.row.verify == 3"
                                    @click="fnVerifyPromot(scope.row.id, 3)"
                                >不通过</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
export default {
    components: { imgPreview },
    data() {
        return {
            authResult: {},
            tableData: [],
            filterData: {
                search_content: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            currTime: 0,
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [
            {
                title: "广告系统",
            },
            {
                title: '广告位列表'
            }
        ]);
        this.filterData.search_content = this.$route.query.search_content ? this.$route.query.search_content : '';
        this.getTableData(this.$route.query.page);
    },
    filters: {
        emptyFilter(val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    },
    methods: {
        fnVerifyPromot(id, verify) {
            let data = {};

            data.verify = verify;
            data.id = id ;
            // 通过
            if (verify == 2) {
                this.$confirm('确认通过该广告位', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(({ value }) => {
                    this.fnVerify(data);
                }).catch(() => {
                });
            } else {
                //拒绝
                this.$prompt('请输入不通过原因', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /\S/,
                    inputErrorMessage: '请输入不通过原因'
                }).then(({ value }) => {
                    data.verify_fail_reason = value;
                    this.fnVerify(data);
                }).catch(() => {
                });
            }
        },
        fnVerify(data){
            this.$post('/wei4/sponsor_activity%5Cverify_adv_config', data, resp => {
                if (resp.code == 1) {
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '操作成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },
        fnResetFrom(formName) {
            this.filterData = {
                aim_user_type: 0,
                type: 0,
                money_type: 0,
                status: '',
                start_time: '',
                end_time: '',
                contract_no: ''
            };
            this.getTableData(1);
        },
        /** 获取表格数据 */
        getTableData(page) {
            let time = new Date().getTime();

            this.currTime = time;

            this.$router.replace({
                query: {
                    ...this.filterData,
                    page: page || this.page.now_page
                }
            });
            this.$post('/wei4/sponsor_activity%5Cget_adv_config_list', {
                ...this.filterData,
                page_id: page || this.page.now_page,
                page_size: 20,
            }, resp => {
                if (this.currTime != time) {
                    return;
                }
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                    if (this.page.now_page > resp.data.page.total_page && this.page.now_page !== 1) {
                        this.getTableData(resp.data.page.total_page);
                    }
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },
        fnOpen(url){
            window.open(url);
        },

        /** 跳转页面 */
        handleRoute(name, query) {
            let obj = {
                name
            };

            if (query) {
                obj.query = query;
            }

            this.$router.push(obj);
        },

        /**  列表渲染辅助函数：列表项为空处理 */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },
        fnShowPreview(images) {
            this.$refs.preview.show([images]);
        },
    }
};
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    flex-direction: column;
    width: 100%;
  }
  .operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;

    .el-button {
      margin-left: 10px;
    }
  }
  .content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
  }
  .search-wrapper {
    padding: 15px;
    background-color: #fff;
  }
  .filter-item {
    .el-form-item {
      white-space: nowrap;
    }
    .el-input {
      width: auto;
      vertical-align: middle;
    }
    .el-button {
      margin-left: 15px;
      vertical-align: middle;
    }
  }
  .table-wrapper {
    flex: 1;
    background-color: #fff;

    .el-button {
      padding: 0;
    }
    .icon-bianji {
      font-size: 14px;
      color: #b9b9b9;
      cursor: pointer;
    }
    .btn-delete {
      color: #fd4d32;
      &.is-disabled {
        color: #c0c4cc;
        cursor: not-allowed;
      }
    }
    .submit-config-item {
      display: inline-block;
      position: relative;
      padding-right: 10px;
      font-size: 14px;

      & + .submit-config-item {
        padding-left: 10px;
        &::before {
          content: "";
          position: absolute;
          top: 50%;
          left: 0;
          width: 1px;
          height: 14px;
          transform: translate(0, -50%);
          background-color: #d8d8d8;
        }
      }

      .num {
        vertical-align: middle;
      }
      .iconfont {
        font-size: 16px;
        color: #b9b9b9;
        vertical-align: middle;
      }

      .icon-tupian {
        font-size: 20px;
      }
    }
  }
  .pagination-wrapper {
    padding-top: 10px;
    .el-pagination {
      float: right;
      padding: 0;
      background-color: transparent;
    }
  }
  .dialog-link {
    & + .dialog-link {
      margin-top: 20px;
    }
    .link-label {
      font-weight: 500;
      margin-right: 20px;
    }
  }
</style>
<style lang="less">
  .activity-list-container {
    .search-wrapper {
      .el-form-item {
        display: inline-block;
      }
    }
  }
</style>
